package com.example.chasshelper.dao;

import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Message;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.utils.OkHttpUtils;
import com.example.chasshelper.utils.TimeUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MessageDao {
    TimeUtils timeUtils = new TimeUtils();
    private OkHttpClient okHttpClient = OkHttpUtils.getClient();
    private String ipUrl = OkHttpUtils.getUrl();
    private String url;

    /*
     * 查询所有
     * */
    public List<Message> findAll() {
        url = ipUrl + "/message/findAll";
        System.out.println("url:"+url);
        List<Message> list = new ArrayList<>();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //传过来的是list，需要用泛型来接收
            Type type = new TypeToken<List<Message>>() {}.getType();
            list = gson.fromJson(string, type);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }


    /*
     * 根据id查询学生用户
     * */
    public Student findStudent(Message message) {
        url = ipUrl + "/message/findStudent?messageId=" + message.getMessageId();
        System.out.println("url:"+url);
        Student student=null;
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //接收数据
            student = gson.fromJson(string, Student.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return student;
    }

    /*
     * 根据id查询教师用户
     * */
    public Teacher findTeacher(Message message) {
        url = ipUrl + "/message/findTeacher?messageId=" + message.getMessageId();
        System.out.println("url:"+url);
        Teacher teacher=null;
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //接收数据
            teacher = gson.fromJson(string, Teacher.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(teacher);
        return teacher;
    }

    /*
     * 发送消息
     * */
    public void insert(String messageContent,Classroom classroom,Teacher teacher,Student student) {
        String messageTime = timeUtils.getTime();
        if (teacher == null) {
            url = ipUrl + "/message/insert?messageContent=" + messageContent + "&messageTime=" + messageTime + "&classroomId=" + classroom.getClassroomId() + "&studentId=" + student.getStudentId();
        } else {
            url = ipUrl + "/message/insert?messageContent=" + messageContent + "&messageTime=" + messageTime + "&classroomId=" + classroom.getClassroomId() + "&teacherId=" + teacher.getTeacherId();
        }
        System.out.println("url:"+url);
        Request request = new Request.Builder().url(url).get().build();
        final Map<String, Object> resultMap = new HashMap<>();
        //执行异步get请求
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                System.out.println("成功");
            }
        });
    }

    /*
     * 根据课堂id查询消息记录
     * */
    public List<Message> findByClassroomId(Classroom classroom) {
        url = ipUrl + "/message/findByClassroomId?classroomId=" + classroom.getClassroomId();
        System.out.println("url:"+url);
        List<Message> list = new ArrayList<>();
        Request request = new Request.Builder().url(url).get().build();
        try {
            //执行同步get请求
            Response response = okHttpClient.newCall(request).execute();
            //获得返回的字符串
            String string =response.body().string();
            //开始解析服务器传过来的json数据
            Gson gson = new Gson();
            //传过来的是list，需要用泛型来接收
            Type type = new TypeToken<List<Message>>() {}.getType();
            list = gson.fromJson(string, type);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }


}
